package cn.com.bob.library.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author Bob
 * @Version：1.0
 * 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取session中的uid
        Object uid = request.getSession().getAttribute("userId");
        //uid为空，则拦截,并且重定向到登录页面
        if(uid == null){
            response.sendRedirect("/");
            return false;
        }

        //未被拦截，放行
        return true;
    }
}
